﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects;
using DC2010;
using DC2010.Objects.Base;
using DC2010.Objects.Data;
using DC2010.Tools;
using DC2010_Editor.Properties;

namespace DC2010_Editor.Controls
{
    public partial class PartyUC : UserControl
    {
        private PartyData data { get; set; }

        public PartyUC()
        {
            InitializeComponent();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            //noone was messing with that
            string id = string.Format(ItemIdTextBox.Text, Editor.I.PartyCount);
            string tempId = BasedOnTextBox.Text;

            PartyData p = new PartyData();
            p.Id = id;
            p.InfoId = tempId;

            Root.I.PartySystem.AddData(p);
            Root.I.PartySystem.SaveData();

            Refresh();
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            ChooseBasedOn chbo = new ChooseBasedOn("Party");
            chbo.ShowDialog();

            if (!string.IsNullOrEmpty(chbo.ChosenAncessor))
            {
                PartyData i = Root.I.PartySystem.GetInfo(chbo.ChosenAncessor).CreateData();

                string tempId = BasedOnTextBox.Text;

                Root.I.PartySystem.AddData(i);
                Root.I.PartySystem.SaveData();

                Refresh();
            }
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Id;
                Root.I.PartySystem.RemoveData(data);
            }

            Refresh();

        }

        private void crystalButton8_Click(object sender, EventArgs e)
        {
            // Remove from system.
            Root.I.PartySystem.RemoveData(data);

            Party i = Root.I.IS.GetParty(data.Info.TemplateId);
            Party iNew = (Party)i.Clone();

            string id = data.Info.Id;
            string tempId = BasedOnTextBox.Text;

            iNew.Info.Id = id;
            iNew.Info.TemplateId = i.Info.Id;

            Root.I.PartySystem.AddToSystem(iNew, id, ObjectTarget.Instance);
            Root.I.PartySystem.SaveInstance();

            Refresh();
        }

        private void Refresh()
        {
            DungeonImageListBox.Items.Clear();

            foreach (Party i in Root.I.PartySystem.ListInstance)
            {
                BaseObject c = i.FindOwner();
                if (c == null)
                {
                    Color clr = Color.Black;
                    if (!i.Validate()) clr = Color.Red;

                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Instanced, i, clr);
                    DungeonImageListBox.Items.Add(itm);
                }
                else
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.InstancedWithOwner, i);
                    DungeonImageListBox.Items.Add(itm);
                }


            }
        }

        private void PartyUC_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void DungeonImageListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBox ilb = (ImageListBox)sender;

            if (ilb.SelectedItem != null)
            {
                if (ilb.SelectedItem is ImageListBoxItem)
                {

                    ImageListBoxItem ilbi = (ImageListBoxItem)ilb.SelectedItem;

                    if (ilbi.Tag is Item)
                    {
                        data = (Party)ilbi.Tag;
                        SetControls();

                        if (string.IsNullOrEmpty(data.Info.TemplateId)) pictureBox1.Image = Resources.tick;
                        if (!string.IsNullOrEmpty(data.Info.TemplateId))
                        {
                            if (Root.I.ItemSystem.Dict.ContainsKey(data.Info.TemplateId))
                                pictureBox1.Image = Resources.tick;
                            else
                                pictureBox1.Image = Resources.cross;
                        }
                    }
                }
            }
        }

        public void SetControls()
        {
            //Ids
            BasedOnTextBox.Text = data.Info.TemplateId;
            ItemIdTextBox.Text = data.Info.Id;
        }

        public void GetFromControls()
        {
            //Ids
            data.Info.TemplateId = BasedOnTextBox.Text;
            data.Info.Id = ItemIdTextBox.Text;
        }
    }
}
